package com.controller.backend;

import com.biz.DeviceInfoBiz;
import com.biz.OperationLogBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.DeviceInfo;
import com.entity.model.ManagerInfo;
import com.entity.model.OperationLog;
import com.util.SystemConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;

/**
 * Created by jc on 2023/03/15.
 */
@Controller
@RequestMapping("/backend/security/deviceInfo")
public class DeviceInfoController extends BaseController<DeviceInfo> {
    @Autowired
    private DeviceInfoBiz deviceInfoBiz;

    @Autowired
    private OperationLogBiz operationLogBiz;

    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/backend/security/deviceInfo/list";
    }

    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/deviceInfo/form";
    }

    @RequestMapping(value = "openDv")
    public String openDv() throws Exception {
        return "/backend/security/deviceInfo/list2";
    }

    @RequestMapping(value = "addDevice")
    @ResponseBody
    public Result addDevice(HttpSession session, DeviceInfo deviceInfo) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        if (deviceInfo != null) {
            baseBiz.save(deviceInfo);

            OperationLog operationLog = new OperationLog();
            operationLog.setManagerId(managerInfo.getId());
            operationLog.setManagerName(managerInfo.getUserName());
            operationLog.setLogType(3);
            operationLog.setLogRemark("新增设备");
            operationLogBiz.save(operationLog);

            return ResultGenerator.genSuccessResult(deviceInfo);
        }
        return ResultGenerator.genFailResult("操作失败");
    }

    /**
     * 更新一个对象
     */
    @RequestMapping(value = "updateDevice")
    @ResponseBody
    public Result updateDevice(HttpSession session, DeviceInfo deviceInfo) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        if (deviceInfo != null) {
            baseBiz.update(deviceInfo);

            OperationLog operationLog = new OperationLog();
            operationLog.setManagerId(managerInfo.getId());
            operationLog.setManagerName(managerInfo.getUserName());
            operationLog.setLogType(4);
            operationLog.setLogRemark("修改设备");
            operationLogBiz.save(operationLog);

            return ResultGenerator.genSuccessResult(deviceInfo);
        }
        return ResultGenerator.genFailResult("操作失败");
    }
}
